class Resource < ActiveRecord::Base

  # validates_format_of :link,
  # :with => /^(https?:\/\/)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)$/i,
  # :message => "invalid url"
  
  #returns the average of the grades of this resource
  def rank()
    begin 
     @grades = Grade.find(:all)
  rescue
    #puts "\n\n> There are no grades in the database <\n\n"
    return 0;
    end
    #puts @grades.length.to_s+" grades found in the database\n\n"
    #compute average:
    mark = i = 0
    @grades.each do |g|
      if (g.resource.id == self.id) then
          mark += g.evaluation 
          i += 1
      end
  end
    #avoid division by 0 (if 0 then divide by 1)
    mark = mark / (i == 0 ? 1 : i)
  end
  
  
  #returns an html snippet which contains
  # the star images to vote the resource
  def voting_interface()
    @rnk = self.rank
    
    embed_html = "<div class=\"grade_a_resource\" id=\"grade_#{@rnk}\">"
    count = 0
    #star_on :
    (1..@rnk).each do |i|
      embed_html << "<a href=\"/resources/vote/#{self.id}:#{count+=1}\"><img src=\"/images/star-l-on.png\"></a>"

    end
    
    #star_off : (n of 'less opaque stars' = 5-rank)
    (1..(5-@rnk)).each do |i|
      embed_html << "<a href=\"/resources/vote/#{self.id}:#{count+=1}\"><img src=\"/images/star-l-off.png\"></a>"
    end
    
    embed_html << "</div>"
    
    return embed_html
  end
  
   def showResourceTags()
#   	 @user = User.find(session[:user_id])
  #  @resource = Resource.find(params[:id])
    query = "Select T.name, T.id from tags T, tag_events  TE  where  TE.resource_id = '#{self.id}' AND T.id = TE.tag_id ORDER BY T.name;"
    tags = Tag.find_by_sql(query)
    if(tags.nil?) then
     tags = []
    end
    
    return tags
  end
  
 def displayShortDescription()
  limitDescrString = 350
  shortDescription = (self.description).slice(0,limitDescrString)
  shortDescription << "..." unless(shortDescription.length >= (self.description).length)
  return shortDescription
 end
 
 def isNewResource()
 	resourceDate = (self.created_at).to_date
 	date = Date.today
 	return ((date-resourceDate) < 7)
 end
 
 def hasVideo
 	answer = false
 	videoTag = Tag.find(:first , :conditions => "name = 'video'")
 	if(not videoTag.nil?) then
 	tag_event = TagEvent.find(:first , :conditions => "tag_id = #{videoTag.id} AND resource_id = #{self.id}")
 	answer = true if(not tag_event.nil?) 
 	end
 	return answer
 end
 
  def hasAudio
 	answer = false
 	videoTag = Tag.find(:first , :conditions => "name = 'audio'")
 	if(not videoTag.nil?) then
 	tag_event = TagEvent.find(:first , :conditions => "tag_id = #{videoTag.id} AND resource_id = #{self.id}")
 	answer = true if(not tag_event.nil?) 
 	end
 	return answer
 end
 
 def returnType
 	width = 320
 	height = 240
 	@vidWidth = width
 	@vidHeight = height
 	templates = ["mov","rm","swf","flv","wmv-wma","wmv-wma","mov","mov","noVideo"]
 	movieTypes = [".mov",".rm",".swf",".flv",".wmv",".wma",".m4v",".mp3"]
 	lengthOfString = (self.link).length
 	fromIndex = lengthOfString - 4
 	toIndex = lengthOfString - 1
 	endString = (self.link)[fromIndex,toIndex]
 	puts endString
 	selectedType = 8
 	pointer = 0
 	while(pointer<movieTypes.length) do
 		selectedType = pointer if(endString.include?(movieTypes[pointer]))
 		pointer += 1
 	end
 	return "videos/"+templates[selectedType]
 end
 

end
